//
//  WebViewController.m
//  TestIos
//
//  Created by 李亚东 on 2025/11/3.
//  Copyright © 2025 Liyadong. All rights reserved.
//

#import "WebViewController.h"
#import <WebKit/WebKit.h>
#import <Masonry/Masonry.h>

@interface WebViewController () <WKUIDelegate, WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *webView;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.view.backgroundColor = [UIColor whiteColor];
    // 配置 WKWebView
    [self setupWebView];
    
    // 加载固定链接
    NSURL *url = [NSURL URLWithString:@"https://m.qa.dttrip.cn/newapp/pages/center/index"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

- (void)setupWebView {
    // 配置 WKWebViewConfiguration
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    
    // 设置允许自动播放媒体
    if (@available(iOS 10.0, *)) {
        configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
    }
    
    // 允许内联媒体播放
    configuration.allowsInlineMediaPlayback = YES;
    
    // 设置偏好设置
    WKPreferences *preferences = [[WKPreferences alloc] init];
    preferences.javaScriptEnabled = YES;
    if (@available(iOS 14.0, *)) {
        preferences.javaScriptCanOpenWindowsAutomatically = YES;
    }
    configuration.preferences = preferences;
    
    // 创建 WKWebView
    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
   
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    [self.view addSubview:self.webView];
    
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(90);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
}

#pragma mark - 内存管理

- (void)dealloc {
    self.webView.UIDelegate = nil;
    self.webView.navigationDelegate = nil;
}

@end
